package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.pojo.entity.User;
import cn.tedu.springmvc.response.JsonResult;
import cn.tedu.springmvc.response.StatusCode;
import cn.tedu.springmvc.service.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户控制器")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    //查询用户信息
    //RESTful风格
//    @RequestMapping(value = "/getUserAll" ,method = RequestMethod.GET)
    @GetMapping("/getUserAll")
    @ApiOperation("查询所有用户")
    public JsonResult getUserAll(){
     //调用业务方法
     List<User> userAll = userService.getUserAll();
     return JsonResult.ok(userAll);
    }
    //新增用户信息
    //@RequestMapping("/saveUser")
    //post +  json{}--->@RequestBody
    @PostMapping("/saveUser")
    @ApiOperation("新增用户")
    public JsonResult saveUser(@RequestBody User user){
        if (user.getUsername().equals("")){
            throw new IllegalArgumentException("username为null");
        }
            //调用业务方法
        userService.saveUser(user);
        return JsonResult.ok();
    }
    //修改用户信息
    @PutMapping("/updateUser")
    @ApiOperation("修改用户")
    //post +  json{}--->@RequestBody
    public String updateUser(@RequestBody User user){
        //调用业务方法
        System.out.println(user);
        userService.updateUser(user);
        return "修改成功";
    }
    //删除用户信息
    @DeleteMapping("/deleteUser")
    @ApiOperation("删除增用户")
    public String deleteUser(Long id){
        //调用业务方法
        userService.deleteUserById(id);
        return "删除成功";
    }

}
